"""
状态类型定义

定义应用中所有的状态类型、状态值和状态选项。
使用枚举和常量确保类型安全和代码一致性。
"""
from enum import Enum, auto
from typing import Dict, List, Any, Union, Optional, Callable


class StateType(str, Enum):
    """状态类型枚举"""
    CONNECTION = "connection"           # 直播间连接状态
    AUDIO_SERVICE = "audio_service"     # 音频服务状态
    AI_SERVICE = "ai_service"           # AI服务状态
    PLAYBACK = "playback"               # 播放状态
    SYNC = "sync"                       # 音画同步状态
    KEYWORD = "keyword"                 # 关键词触发状态
    USER_JOIN = "user_join"             # 用户进入状态
    AUDIO_OUTPUT = "audio_output"       # 音频输出设备状态


class StateValues:
    """状态值常量"""
    # 连接状态
    CONNECTED = "已连接"
    CONNECTING = "连接中"
    DISCONNECTED = "未连接"
    ERROR = "连接错误"
    
    # 服务状态
    ENABLED = "已启用"
    DISABLED = "未启用"
    STARTED = "已启动"
    STOPPED = "已停止"
    FAILED = "启动失败"
    
    # 播放状态
    PLAYING = "播放中"
    READY = "就绪"
    PAUSED = "暂停"
    
    # 同步状态
    SYNC_ON = "已开启"
    SYNC_OFF = "未开启"


class StateOptions:
    """状态选项定义，包含每种状态的有效值和默认值"""
    
    OPTIONS = {
        StateType.CONNECTION: {
            "valid_values": [
                StateValues.CONNECTED, 
                StateValues.CONNECTING, 
                StateValues.DISCONNECTED, 
                StateValues.ERROR
            ],
            "default": StateValues.DISCONNECTED,
            "priority": 10
        },
        StateType.AUDIO_SERVICE: {
            "valid_values": [
                StateValues.CONNECTED, 
                StateValues.DISCONNECTED, 
                StateValues.CONNECTING, 
                StateValues.ERROR, 
                StateValues.FAILED
            ],
            "default": StateValues.DISCONNECTED,
            "priority": 20
        },
        StateType.AI_SERVICE: {
            "valid_values": [
                StateValues.ENABLED, 
                StateValues.DISABLED, 
                StateValues.CONNECTED, 
                StateValues.DISCONNECTED
            ],
            "default": StateValues.DISABLED,
            "priority": 30,
            "can_be_overridden": True  # AI服务状态可被播放状态覆盖
        },
        StateType.PLAYBACK: {
            "valid_values": [
                StateValues.PLAYING, 
                StateValues.READY, 
                StateValues.PAUSED
            ],
            "default": StateValues.READY,
            "priority": 40,
            "overrides": [StateType.AI_SERVICE],  # 播放状态会覆盖AI服务状态
            "prefix_allowed": True  # 允许使用前缀，如"播放中: 欢迎语"
        },
        StateType.SYNC: {
            "valid_values": [
                StateValues.SYNC_ON, 
                StateValues.SYNC_OFF
            ],
            "default": StateValues.SYNC_OFF,
            "priority": 50
        },
        StateType.KEYWORD: {
            "valid_values": [],  # 任何值都可接受
            "default": "30-60秒",
            "priority": 60,
            "prefix_allowed": True
        },
        StateType.USER_JOIN: {
            "valid_values": [],  # 任何值都可接受
            "default": "30-60秒",
            "priority": 70,
            "prefix_allowed": True
        },
        StateType.AUDIO_OUTPUT: {
            "valid_values": [],  # 任何设备名称都可接受
            "default": "默认设备",
            "priority": 80
        }
    }
    
    @staticmethod
    def get_default(state_type: StateType) -> str:
        """获取指定状态类型的默认值"""
        return StateOptions.OPTIONS.get(state_type, {}).get("default", "")
    
    @staticmethod
    def is_valid_value(state_type: StateType, value: str) -> bool:
        """验证状态值是否有效"""
        valid_values = StateOptions.OPTIONS.get(state_type, {}).get("valid_values", [])
        
        # 如果没有指定有效值，则任何值都接受
        if not valid_values:
            return True
            
        # 检查前缀
        prefix_allowed = StateOptions.OPTIONS.get(state_type, {}).get("prefix_allowed", False)
        if prefix_allowed:
            for valid in valid_values:
                if value == valid or value.startswith(f"{valid}:"):
                    return True
            return False
                
        # 普通检查
        return value in valid_values
    
    @staticmethod
    def get_priority(state_type: StateType) -> int:
        """获取状态类型的优先级"""
        return StateOptions.OPTIONS.get(state_type, {}).get("priority", 0)
    
    @staticmethod
    def get_overrides(state_type: StateType) -> List[StateType]:
        """获取此状态类型可以覆盖的其他状态类型"""
        return StateOptions.OPTIONS.get(state_type, {}).get("overrides", [])
    
    @staticmethod
    def can_be_overridden(state_type: StateType) -> bool:
        """检查此状态类型是否可以被其他状态覆盖"""
        return StateOptions.OPTIONS.get(state_type, {}).get("can_be_overridden", False) 